var path = require("path");
var express = require('express');
var bodyParser = require('body-parser');
var ext = require("./common/ext");


var server = express(),
    config,logger;

server.use(bodyParser.urlencoded({extended: false}));
server.use(bodyParser.json());

module.exports = function(config_){
    config = config_;

    logger = require("./platform/platform-logger").getLog(path.resolve(__dirname,".."),"server");
    logger.setLevel('debug');

    var app_loader = require("./app/app-loader")({
        server:server,
        config:config,
        logger:logger
    });

    var platform_loader = require("./platform/platform-loader")({
        server:server,
        config:config,
        logger:logger
    });

    return {
        server:server,
        logger:logger,
        load_app:app_loader.load,
        load_platform:platform_loader.load,
        start:start
    };
}

/*
  start server
*/
function start(){
    if(config.development){        
        server.use("/web-lib",express.static(path.resolve(__dirname,"..",config.web_lib_root)));
    }

    require("./common/api-upload").route(server);

    server.listen(config.port);
    logger.info("server listen on "+config.port);    
}

